#include "SceneTree.h"
#include "TreeFunctors.h"

CSceneNode::CSceneNode(CSceneNode *a_Parent):
m_L2W(*(new maths::CMatrix4x4f)), m_bL2WCreated(true)
{
	if (a_Parent != NULL)
	{
		a_Parent->AddChild(this);
		m_bL2WChanged = true;
	}
	else
	{
		m_pParent = NULL;
		m_bL2WChanged = false;
	}
}

CSceneNode::CSceneNode(maths::CMatrix4x4f &a_L2W, CSceneNode *a_Parent):
m_L2W(a_L2W), m_bL2WCreated(false), m_bL2WChanged(true)
{
	if (a_Parent != NULL)
		a_Parent->AddChild(this);
	else
		a_Parent = NULL;
}

CSceneNode::~CSceneNode ( )
{
	if (m_bL2WCreated)
		delete &m_L2W;
	m_Childs.clear();
}

CSceneNode *CSceneNode::Parent( )
{
	return m_pParent;
}

mVoid CSceneNode::AddChild(CSceneNode *a_Node)
{
	a_Node->m_pParent = this;
	m_Childs.push_back(a_Node);
}

mVoid CSceneNode::UpdateL2W()
{
	if (m_bL2WChanged)
	{
		m_L2W = m_L2P * m_pParent->m_L2W;
		m_bL2WChanged = false;
	}
}

mBool CSceneNode::SetL2WChanged()
{
	if (m_bL2WChanged)
		return true;
	m_bL2WChanged = true;
	return false;
}

//mVoid CSceneNode::SetL2W(maths::CMatrix4x4f &a_Mtx)
//{
//    m_L2W = a_Mtx;
//}
//
//mVoid CSceneNode::SetL2P(maths::CMatrix4x4f &a_Mtx)
//{
//    m_L2P = a_Mtx;
//}

maths::CMatrix4x4f CSceneNode::GetL2P()
{
	return m_L2P;
}

mVoid CSceneNode::SetL2P( maths::CMatrix4x4f &a_L2P )
{
	m_L2P = a_L2P;
	TreeFunctor::NotifyL2WChanged Notification;
	CSceneTree::Iterate(Notification, this);
}

maths::CMatrix4x4f CSceneNode::GetL2W()
{
	return m_L2W;
}

maths::CAABox &CSceneNode::BoundBox()
{
	return m_AABoundBox;
}